<?php
require_once  '../classes/ConnectionDB.php';
require_once  '../classes/TestDataManager.php';
require_once  '../classes/LoadProperties.php';

$properties= new LoadProperties();

$properties->initServices();

$connection =ConnectionDB::getInstance()->connectToDB();

$upload_dir = $_SERVER["DOCUMENT_ROOT"] . "school/uploaded";
$file_name =  $_POST["code"].'-'.$_FILES["fileTmp"]["name"];
$codiceDomanda = $_POST["code"];

if(trim($_FILES["fileTmp"]["name"]) == "")
{
    die("Non hai indicato il file da uploadare !");
}


if(@is_uploaded_file($_FILES["fileTmp"]["tmp_name"]))
{
    @move_uploaded_file($_FILES["fileTmp"]["tmp_name"], "$upload_dir/$file_name")
              or die("Impossibile spostare il file, controlla l'esistenza o i permessi della directory dove fare l'upload.");
    $link = $properties->getUploadPath() .'/'.  $_POST["code"].'-'.$_FILES["fileTmp"]["name"];
    TestDataManager::getInstance()->updateLinkFromQuestion($connection, $codiceDomanda, $link);

}
else
{
    die("Problemi nell'upload del file " . $_FILES["fileTmp"]["name"]);
}

//echo "L'upload del file " . $_FILES["fileTmp"]["name"] . " è avvenuto correttamente";
if($operation == 'updateAttachment')
{
    $link =  $properties->getUploadPath() .'/'. $file_name;
    TestDataManager::getInstance()->updateLinkFromQuestion($connection, $codiceDomanda, $link);
    // echo 'link ' . $link .' codice dom ' . $codiceDomanda;

    $str="Location:../report/detailQuestion.php?codiceDomanda=" . $_POST["code"];
}
else
    $str="Location:newQuestionUpload.php";
header($str);

?>
